package com.hq.fxh.No1_Infromation.Infor.Adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.PopupWindow;

import com.bumptech.glide.Glide;
import com.hq.fxh.No1_Infromation.Infor.Activity.NewsDetailsActivity;
import com.hq.fxh.R;
import com.hq.fxh.Utils.DensityUtils;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SystemUtils;
import com.hq.fxh.Utils.Tool;
import com.hq.fxh.CustomerView.PopupWindow_PingBi;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/8/9
 * @Email xlwenhao1858@sina.com
 */


public class Help_Infor_NewsType1 {
    Context context;
    Adapter_recycle_infor.NewsType1ViewHolder holder;
    RecyclerView recyclerView;

    public void setRecyclerView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
    }

    public Help_Infor_NewsType1(Context mcontext, Adapter_recycle_infor.NewsType1ViewHolder mholder) {
        context = mcontext;
        holder = mholder;
    }

    public void doForNewsType1(int type) {
        if (type == -1) {
            LogUtils.e("newsType1中初始化type失败");
        } else if (type == 1) {
            /*新闻类型为新闻流*/
            holder.newsType1LinyoutAD.setVisibility(View.GONE);
            holder.newsType1LinyoutNoAD.setVisibility(View.VISIBLE);
            holder.NewsType1LinyoutCopyRight.setVisibility(View.VISIBLE);
            holder.NewsType1LinyoutAuthor.setVisibility(View.GONE);
            holder.news_author_tag.setVisibility(View.GONE);
        } else if (type == 2) {
             /*新闻类型为论坛新闻*/
            holder.newsType1LinyoutAD.setVisibility(View.GONE);
            holder.newsType1LinyoutNoAD.setVisibility(View.VISIBLE);
            holder.NewsType1LinyoutCopyRight.setVisibility(View.GONE);
            holder.NewsType1LinyoutAuthor.setVisibility(View.VISIBLE);
            holder.news_author_tag.setVisibility(View.VISIBLE);

        } else if (type == 3) {
             /*新闻类型为广告*/
            holder.newsType1LinyoutAD.setVisibility(View.VISIBLE);
            holder.newsType1LinyoutNoAD.setVisibility(View.GONE);


        } else {
            LogUtils.e("newsType1中type值异常");
        }
        setMyClick(context);

        Glide.with(context).load("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3948656430,2360060343&fm=26&gp=0.jpg").into(holder.newImageView);

    }

    private void setMyClick(final Context mcontext) {
        holder.newsType1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mcontext, NewsDetailsActivity.class);
                mcontext.startActivity(intent);
            }
        });

        holder.pingbi.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View view) {
                 View popview = View.inflate(context, R.layout.popup_information_pingbi, null);
                new PopupWindow_PingBi().showPopupWindow((Activity) context, popview, holder.news_author_tag);


            }

        });
    }

    private void ddin() {
        int[] location = new int[2];
        holder.news_author_tag.getLocationOnScreen(location);
        int x = location[0];
        int y = location[1];
        int windowHeight = SystemUtils.getWindowHeight((Activity) context);
        int windowWidth = SystemUtils.getWindowWidth((Activity) context);

        LogUtils.e("x:" + x + "y:" + y + "   高度" + windowHeight);

        View popview = View.inflate(context, R.layout.popup_information_pingbi, null);
                /* popview.getMeasuredWidth()*/
        popview.measure(0, 0);
        PopupWindow pop = new PopupWindow(popview, windowWidth - DensityUtils.dip2px(context, 20), popview.getMeasuredHeight(), true);
        pop.getHeight();
        pop.getWidth();

        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setAnimationStyle(R.style.dialog_anim_style);
        // 指定 PopupWindow 的背景
        pop.setFocusable(true);
        int height = pop.getHeight();
        LogUtils.e("pop高度" + height);

        //指定PopupWindow显示在你指定的view下

        if (windowHeight / 3 > (windowHeight - y)) {
            popview.findViewById(R.id.popup_PingBi).setBackgroundResource(R.mipmap.tan_shou_to);

            pop.showAtLocation(recyclerView, Gravity.TOP, 0, (y - height));

        } else {
            popview.findViewById(R.id.popup_PingBi).setBackgroundResource(R.mipmap.tan_shou);

            pop.showAtLocation(recyclerView, Gravity.TOP, 0, (y + 40));
        }
        Tool.setAlpha((Activity) context, 0.7f);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                Tool.setAlpha((Activity) context, 1f);
            }
        });
    }


}
